package EXEX;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.ImageIcon;

import network.MultiChatServer;
import network.SocketData;

public class rspserverThread extends Thread {
	rspserver rs;
	Socket socket;
	boolean tFlag = true;
	ObjectOutputStream send;
	ObjectInputStream receive;
	
	int serversel = 0;  //서버가 낸값
	int usersel = 0;    // 유저가 낸값
	String result;      // 가위바위보 결과
	int gamestr = 0;
	
	int win;
	int draw;
	int lose;
	
	int timer = 11;
	boolean timerflag = false;

	public rspserverThread(rspserver s, Socket socket) {
		this.rs = s;
		this.socket = socket;
	}

	public void run() {
		try {
			send = new ObjectOutputStream(socket.getOutputStream());
			receive = new ObjectInputStream(socket.getInputStream());
			
			while (tFlag) {
				rspData rd = (rspData) receive.readObject();
				if (rd.getCommand().equals("login")) {
					rs.myClients.put(rd.getUserid(), this);
					rs.username.setText(rd.userid);
					rs.userstatus.setText("준비중...");
					rs.userlbl.setIcon(new ImageIcon("D:\\user.jpg"));
				}
				else if(rd.getCommand().equals("ready")){
					rs.userstatus.setText("준비완료!");
					rs.gamestartbtn.setEnabled(true);
				}
				else if(rd.getCommand().equals("gameStart")){
					timerflag = true;
					timerrun();
					timerflag = false;
					timer = 11;
					if(serversel == 0){
						serversel = (int)(Math.random()*3) + 1;
						rs.rbtn.setEnabled(false);
						rs.sbtn.setEnabled(false);
						rs.pbtn.setEnabled(false);
					}
				}
				else if(rd.getCommand().equals("select") && rd.getSelect() != 0 && serversel != 0){
					usersel = rd.getSelect();
					rspresult();
					serversel = 0;  //서버가 낸것을 초기화 시킴
					send.writeObject(rd);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void rspresult(){
		try{
			if(serversel == usersel){
				result = "draw";
				rspData rd = new rspData("result", "Server", result);
				send.writeObject(rd);
				draw++;
			}
			else if(serversel == 1 && usersel == 2){  // 서버 가위 클라이언트 바위
				result = "lose";
				rspData rd = new rspData("result", "Server", result);
				send.writeObject(rd);
				lose++;
			}
			else if(serversel == 1 && usersel == 3){
				result = "win";
				rspData rd = new rspData("result", "Server", result);
				send.writeObject(rd);
				win++;
			}
			else if(serversel == 2 && usersel == 1){
				result = "win";
				rspData rd = new rspData("result", "Server", result);
				send.writeObject(rd);
				win++;
			}
			else if(serversel == 2 && usersel == 3){
				result = "lose";
				rspData rd = new rspData("result", "Server", result);
				send.writeObject(rd);
				lose++;
			}
			else if(serversel == 3 && usersel == 1){
				result = "lose";
				rspData rd = new rspData("result", "Server", result);
				send.writeObject(rd);
				lose++;
			}
			else if(serversel == 3 && usersel == 2){
				result = "win";
				rspData rd = new rspData("result", "Server", result);
				send.writeObject(rd);
				win++;
			}
			rs.serverresult.setText(result);
			rs.result1.setText("전적 : " + win + " 승 " + draw + " 무 " + lose + " 패 ");
			serversel = 0;
			usersel = 0;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	
	public void timerrun(){
		try{
			while(timerflag && timer > 0){
				timer -= 1;
				rs.time1.setText("남은시간 : " + Integer.toString(timer));
				Thread.sleep(1000);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

}
